package com.ww.springboot.boot.spring;

import com.ww.springboot.boot.spring.dto.Bean1;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.lang.reflect.Field;

/**
 * todo
 *
 * @author wanwei
 * @since 2021-02-18 10:19
 */
public class BeanDefinitionDemo {

    public static void main(String[] args) {

        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(Bean1.class);
        beanDefinitionBuilder
                .addPropertyValue("str", "str1");

        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration Class（配置类）
        applicationContext.register(BeanDefinitionDemo.class);

        BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinitionBuilder.getBeanDefinition(), applicationContext);

        // 启动 Spring 应用上下文
        applicationContext.refresh();
        // 按照类型依赖查找
        System.out.println(applicationContext.getBeansOfType(Bean1.class));
        // 显示地关闭 Spring 应用上下文
        applicationContext.close();






    }
}
